//
//  DevFormButtonField.swift
//  DevFormView
//
//  Created by dev on 15/9/18.
//  Copyright (c) 2015年 devilved. All rights reserved.
//

import UIKit

class DevFormButtonField: DevFormField {
 
    var button:UIButton?
    
    var buttonTextFont:UIFont?
    
    var buttonBackgroundColor:UIColor?
    
    var buttonTitleColor:UIColor?
    
    var onClick:(DevFormButtonField) -> Void = {(formButtonField:DevFormButtonField) -> Void in }
    
    override func initDevFormFieldView() {
        let left:CGFloat = self.formTableView.separatorInset.left > 0 ? self.formTableView.separatorInset.left : 15
        let right:CGFloat = self.formTableView.separatorInset.right > 0 ? self.formTableView.separatorInset.right : 15
        
        self.view = UIView()
        let main:UIView = self.view!
        main.frame = CGRectMake(0, 0, self.formTableView.frame.width, height)
        main.backgroundColor = self.formTableView.backgroundColor
        self.button = UIButton()
        let button = self.button!
        if let font:UIFont = self.buttonTextFont {
            button.titleLabel?.font = font
        }
        //let size:CGSize = DevStringUtils.sizeOfString(self.label, font: button.titleLabel!.font)
        if let buttonBackgroundColor = self.buttonBackgroundColor {
            button.backgroundColor = buttonBackgroundColor
        } else {
            button.backgroundColor = UIColor(red: 54 / 255, green: 184 / 255, blue: 246 / 255, alpha: 1)
        }
        if let buttonTitleColor:UIColor = self.buttonTitleColor {
            button.setTitleColor(buttonTitleColor, forState: UIControlState.Normal)
        } else {
            button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        }
        self.value = self.defaultValue
        button.setTitle(self.label, forState: UIControlState.Normal)
        button.frame = CGRectMake(left, (self.height - self.controlHeight) / 2, self.formTableView.frame.width - left - right, self.controlHeight)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = self.controlHeight / 4
        main.addSubview(button)
        button.addTarget(self, action: "onButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    override func initConstraints() {
        let main:UIView = self.view!
        let button = self.button!
        //let size:CGSize = DevStringUtils.sizeOfString(self.label, font: button.titleLabel!.font)
        let left:CGFloat = self.formTableView.separatorInset.left > 0 ? self.formTableView.separatorInset.left : 15
        let right:CGFloat = self.formTableView.separatorInset.right > 0 ? self.formTableView.separatorInset.right : 15
        
        button.translatesAutoresizingMaskIntoConstraints = false

        main.addConstraint(NSLayoutConstraint(item:button, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: self.controlHeight))
        
        main.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: main, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: left))
        
        main.addConstraint(NSLayoutConstraint(item: main, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: button, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: right))
        
        main.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: main, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    }
    
    override func isAnimated() -> Bool {
        return false
    }
    
    override func click() {
        super.click()
    }
    
    override func focus() {
        self.formTableView.endEditing(true)
        if self.button!.enabled {
            self.onClick(self)
        }
    }
    
    func onButtonClick() {
        self.focus()
    }
}
